﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Enums;
using UIWindow.Helper;

namespace UIWindow.Protocols.Fins
{
    public class FinsWrite : OmRonFinsBase
    {
        /// <summary>
        /// PLC网络号 一般为0x00
        /// </summary>
        public byte DNA { get; set; } = 0x00;
        /// <summary>
        /// 服务器节点（握手包返回）
        /// </summary>
        public byte DA1 { get; set; }
        /// <summary>
        /// PLC单元号
        /// </summary>
        public byte DA2 { get; set; } = 0x00;
        /// <summary>
        /// 本机网络号
        /// </summary>
        public byte SNA { get; set; } = 0x00;
        /// <summary>
        /// 本机节点 IP最后一位
        /// </summary>
        public byte SA1 { get; set; }
        /// <summary>
        /// 源单元号 与目标一致
        /// </summary>
        public byte SA2 { get; set; } = 0x00;
        /// <summary>
        /// 操作区域
        /// </summary>
        public OmronOperatingAreaType OperatingArea { get; set; }

        /// <summary>
        /// 读写地址
        /// </summary>
        public ushort Address { get; set; }

        /// <summary>
        /// 位地址（相当于偏移）
        /// </summary>
        public byte ByteAddress { get; set; } = 0;

        /// <summary>
        /// 读写长度
        /// </summary>
        public ushort ProtoLength { get; set; }

        /// <summary>
        /// 写数据
        /// </summary>
        public List<ushort> DataList { get; set; }
        public FinsWrite() : base()
        {
        }
        public override byte[] GetStream()
        {
            //发送数据长度：命令码开始的字节数量
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x1a);

            //命令码
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            //base._memoryStream.WriteByte(0x01);
            //base._memoryStream.WriteByte(0x02);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x02);

            //错误代码
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);

            //Fins
            base._memoryStream.WriteByte(0x80);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x02);
            base._memoryStream.WriteByte(DNA);
            base._memoryStream.WriteByte(DA1);
            base._memoryStream.WriteByte(DA2);
            base._memoryStream.WriteByte(SNA);
            base._memoryStream.WriteByte(SA1);
            base._memoryStream.WriteByte(SA2);
            base._memoryStream.WriteByte(0x00);//SID固定

            //0x01 0x02 写入
            base._memoryStream.WriteByte(0x01);
            base._memoryStream.WriteByte(0x02);


            //操作区域
            base._memoryStream.WriteByte((byte)OperatingArea);

            //起始地址
            base._memoryStream.Write(Address.ToString("X4").StrToHexByte(), 0, 2);

            //位移
            base._memoryStream.WriteByte(ByteAddress);

            //写数量
            base._memoryStream.Write(ProtoLength.ToString("X4").StrToHexByte(), 0, 2);

            //数据
            foreach (var item in DataList)
            {
                base._memoryStream.Write(item.ToString("D4").StrToHexByte(), 0, 2);
            }

            byte[] bytes = base.GetStream();
            bytes[7] = (byte)((bytes.Length - 8) & 0xff);
            return bytes;
        }
    }
}
